<?php

	/**
	 * @see Zend_Controller_Plugin_Abstract
	 */
	require_once 'Zend/Controller/Plugin/Abstract.php';
	
	/**
	 * @see Zend_Controller_Front
	 */
	require_once 'Zend/Controller/Front.php';
	
	/**
	 * @see Zend_Reflection_Class
	 */
	require_once 'Zend/Reflection/Class.php';
	
	
	abstract class Bike_Plugin_Abstract extends Zend_Controller_Plugin_Abstract
    {
        const CONFIG_PLUGIN_NAME = null;

        protected $_reflection;
        protected $_configPluginName;
		protected $_application;

        protected function getFrontController()
        {        	return Zend_Controller_Front::getInstance();
        }

        public function setApplication(Zend_Application $Application)
        {        	$this->_application = $Application;
        	return $this;
        }

        protected function getApplication()
        {        	if(is_null($this->_application))
        	{        		return $this->getFrontController()->getBootstrap()->getApplication();
        	}
        	return $this->_application;
        }

        protected function getReflection()
        {        	if(is_null($this->_reflection))
        	{        		$this->_reflection = new Zend_Reflection_Class($this);
        	}
        	return $this->_reflection;
        }

        protected function getConfigPluginName()
        {        	if(is_null($this->_configPluginName))
        	{        		$this->_configPluginName = $this->getReflection()->getConstant('CONFIG_PLUGIN_NAME');
        	}
        	if(is_null($this->_configPluginName))
        	{        		require_once 'Bike/Plugin/Exception.php';
        		throw new Bike_Plugin_Exception('Undefined or incorrect const '.get_class($this).'::CONFIG_PLUGIN_NAME');
        	}
        	return $this->_configPluginName;
        }

        protected function getOptions()
        {
        	$configPluginName = $this->getConfigPluginName();
        	$options = $this->getApplication()->getOptions();
        	return $options['plugins'][$configPluginName];
        }

        abstract public function runForCli(Zend_Application $Application);
    }
